package com.bw.xiaosy.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.PostConstruct;
import java.util.Set;

/**
 * @author Xiaosy
 * @date 2017-03-30 16:44
 */
@Service
public class RedisManager {

    @Autowired
    private RedisConfiguration conf;

    private static JedisPool jedisPool = null;

    public RedisManager() {
    }

    /**
     * 初始化方法
     */
    @PostConstruct
    public void init()
    {
        if(jedisPool == null)
        {
            if(conf.getPassword() != null && !"".equals(conf.getPassword()))
            {
                jedisPool = new JedisPool(new JedisPoolConfig(),conf.getHost(),conf.getPort(),conf.getTimeout(),conf.getPassword());
            }else if(conf.getTimeout() != 0)
            {
                jedisPool = new JedisPool(new JedisPoolConfig(),conf.getHost(),conf.getPort(),conf.getTimeout());
            }else{
                jedisPool = new JedisPool(new JedisPoolConfig(),conf.getHost(),conf.getPort());
            }
        }
    }

    /**
     * get value from redis
     * @param key
     * @return
     */
    public byte[]get(byte[]key)
    {
        byte[] value = null;
        Jedis jedis = jedisPool.getResource();
        try {
            value = jedis.get(key);
        }finally {
            jedis.close();
        }
        return value;
    }

    /**
     * set
     * @param key
     * @param value
     * @return
     */
    public byte[]set(byte[]key,byte[]value)
    {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(key,value);
            if(conf.getExpire() != 0)
            {
                jedis.expire(key,conf.getExpire());
            }
        }finally {
            jedis.close();
        }
        return value;
    }
    /**
     * set
     * @param key
     * @param value
     * @param expire
     * @return
     */
    public byte[] set(byte[] key,byte[] value,int expire){
        Jedis jedis = jedisPool.getResource();
        try{
            jedis.set(key,value);
            if(expire != 0){
                jedis.expire(key, expire);
            }
        }finally{
            jedis.close();

        }
        return value;
    }
    /**
     * del
     * @param key
     */
    public void del(byte[] key){
        Jedis jedis = jedisPool.getResource();
        try{
            jedis.del(key);
        }finally{
            jedis.close();

        }
    }
    /**
     * flush
     */
    public void flushDB(){
        Jedis jedis = jedisPool.getResource();
        try{
            jedis.flushDB();
        }finally{
            jedis.close();

        }
    }
    /**
     * size
     */
    public Long dbSize(){
        Long dbSize = 0L;
        Jedis jedis = jedisPool.getResource();
        try{
            dbSize = jedis.dbSize();
        }finally{
            jedis.close();

        }
        return dbSize;
    }
    /**
     * keys
     * @param
     * @return
     */
    public Set<byte[]> keys(String pattern){
        Set<byte[]> keys = null;
        Jedis jedis = jedisPool.getResource();
        try{
            keys = jedis.keys(pattern.getBytes());
        }finally{
            jedis.close();

        }
        return keys;
    }

    public static JedisPool getJedisPool() {
        return jedisPool;
    }

    public static void setJedisPool(JedisPool jedisPool) {
        RedisManager.jedisPool = jedisPool;
    }
}
